﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MessageCommon;
using MessageCommon.GlobalType;
using ReceiveClient.SendMessage;
using MessageCommon.GlobalStructure;
using System.Runtime.Remoting.Channels.Tcp;
using System.Runtime.Remoting.Channels;
using System.Messaging;
using System.Net.Sockets;
using System.Net;
using System.Runtime.Remoting;
using System.Collections;
using System.Runtime.Remoting.Channels.Http;

namespace ReceiveClient
{
    public class ClientManager
    {
        SendBase m_objPCSend;
        ClientReceiveMSG m_objMSGReceive;
        public ClientManager()
        {
            m_objPCSend = new SendMSGToPCClient();
            m_objMSGReceive = new ClientReceiveMSG();
        }

        #region 注册
        HttpClientChannel m_tcpChannel;
        public void RegisterUser(AddressInfo userAddress)
        {
            if (m_tcpChannel == null)
            {
                string strChannelName = string.Format("{0}_{1}", GlobalFunc.GetCpuSerialNum(), userAddress.strUserId);
                m_tcpChannel = new HttpClientChannel(strChannelName, new BinaryClientFormatterSinkProvider());
            }
            ChannelServices.RegisterChannel(m_tcpChannel, false);
            ClientBase.Instance.ClientObject.RegisterUser(ClientBase.Instance.UsersID, userAddress);
        }

        public void UnRegisterUser(AddressInfo address)
        {
            ChannelServices.UnregisterChannel(m_tcpChannel);
            ClientBase.Instance.ClientObject.UnRegisterUser(ClientBase.Instance.UsersID, address);
        }

        #endregion

        #region  发送信息

        private AddressInfoList GetUserOnLineAddress(string strUserName)
        {
            return ClientBase.Instance.ClientObject.GetUserOnlineAddress(strUserName);
        }

        public bool SendOffLineMessage(PushMessageList msgList)
        {
            return false;
        }

        public bool SendOnlineMessage(PushMessageList msgList)
        {
            foreach (var item in msgList)
            {
                foreach (var target in item.strTarget)
                {
                    foreach (var address in GetUserOnLineAddress(target))
                    {
                        m_objPCSend.SendOnlineMessage(item, address);
                    }
                }
            }
            return true;
        }

        private void SendOnlineMessage(PushMessage msg)
        {
            foreach (var item in msg.strTarget)
            {
                foreach (var address in GetUserOnLineAddress(item))
                {
                    switch (address.enumClientType)
                    {
                        case EnumClientType.PCClient:
                            m_objPCSend.SendOnlineMessage(msg, address);
                            break;
                        default:
                            break;
                    }
                }
            }
        }

        #endregion

        #region 接受信息

        public void ReceiveOnLineMSG()
        {
            m_objMSGReceive.ReceiveOnLineMSG();
        }

        public void StopReceiveMsg()
        {
            m_objMSGReceive.StopReceiveMsg();
        }

        public void ReceiveOffLineMSG()
        {
            m_objMSGReceive.ReceiveOffLineMSG();
        }
        #endregion
    }
}
